package org.gvp.generator.core.writer.impl;

import org.gvp.generator.core.WriterToResources;
import org.gvp.generator.core.writer.AbstractFileWriter;
import org.gvp.generator.props.CreateProps;
import org.gvp.generator.props.ProjectProps;
import org.gvp.generator.table.TableInfo;

import java.nio.file.Path;
import java.nio.file.Paths;

@WriterToResources
public class TsTableColumnsFileWriter extends AbstractFileWriter {
    @Override
    public void execute(TableInfo tableInfo, ProjectProps project) {
        String upName = tableInfo.getUpperName();
        String imps = """
                import {Space} from "antd";
                import {ColumnsType} from "antd/es/table";
                import {Link} from "react-router-dom";
                import {%s} from "type/entity/%s";
                import {columnDataIndex, columnOrder, columnRemark} from "./index";
                
                """.formatted(upName,upName);
      channel.writeNewline(imps);
      channel.writeNewline("const %sColumns: ColumnsType<%s> = [".formatted(tableInfo.getLowerName(),upName));
      channel.writeNewline("    columnDataIndex,");
      tableInfo.getColumns().forEach(col -> {
          channel.write("""
                      {
                          key: '%s',
                          dataIndex: '%s',
                          title: '%s',
                          width: 110,
                          align: "center",
                      },
                  """.formatted(col.getLowerName(),col.getLowerName(),col.getRemark()));
      });
      channel.writeNewline("""
                  columnOrder,
                  columnRemark,
              """);
      channel.writeNewline("""
                  {
                      key: "options",
                      title: "操作",
                      dataIndex: "options",
                      align: "center",
                      width: 130,
                      fixed: "right",
                      render: (_, record, __) => {
                          return <Space size={15}>
                              <Link key={"%s-details"} to={"details"} state={record}>详情</Link>
                              <Link key={"%s-update"} to={"update"} state={record}>更新</Link>
                              <Link key={"%s-delete"} style={{color: "#c13c3c"}} to={"delete"} state={record}>删除</Link>
                          </Space>;
                      }
                  }
              """.formatted(tableInfo.getLowerName(),tableInfo.getLowerName(),tableInfo.getLowerName()));
      channel.writeNewline("];");

    }

    @Override
    public boolean isWriterFile(CreateProps create) {
        return create.isCreateTsTableColumns();
    }

    @Override
    public Path getFilePath(String basePath, ProjectProps project, String fileName) {
        return Paths.get(basePath,project.getTypescriptDirPath(),fileName + "Columns.tsx");
    }
}
